/*
preferences.cpp
(c) 2010 Yury Hamenkov
This file is part of qWordCards.

qWordCards is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

qWordCards is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with qWordCards. If not, see <http://www.gnu.org/licenses/>.
*/

#include "preferences.h"

#include <QFile>
#include <QDebug>
#include "dictionary.h"

using namespace qWordCards;

/* static */
bool Preferences::firstTime;

/* static */
int Preferences::wordShowDuration;
/* static */
int Preferences::wordChangeInterval;
/* static */
int Preferences::showTranslationInterval;

/* static */
unsigned short int Preferences::wordsQueueLength = 20;

/* static */
unsigned short int Preferences::favouriteWordProbability = 40;

/* static */
bool Preferences::showToolbar = true;
/* static */
QColor Preferences::mainWindowBackColor (255, 255, 168);;
/* static */
QSize Preferences::mainWindowSize (300, 300);
/* static */
QPoint Preferences::mainWindowPosition (100, 100);
/* static */
WordsList Preferences::forbiddenWords;
/* static */
WordsList Preferences::favouriteWords;
/* static */
WordsList Preferences::suspendedWords;

/* static */
QFont Preferences::wordFont;
/* static */
QFont Preferences::transcriptionFont;
/* static */
QFont Preferences::translationFont;

/* static */
QColor Preferences::wordColor (0, 0, 0);
/* static */
QColor Preferences::transcriptionColor (0, 0, 0);
/* static */
QColor Preferences::translationColor (0, 0, 0);

/* static */
QTranslator* Preferences::currentTranslator = 0;

/* static */
void Preferences::Save() {
	QFile f("./settings.cfg");
	
	qint64 result = 0;
	int int_value = 0;
	bool bool_value = false;
	QString str_value;

	if (f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
		result = f.write((char*)&Preferences::firstTime, sizeof(Preferences::firstTime));
		result = f.write((char*)&Preferences::wordShowDuration, sizeof(Preferences::wordShowDuration));
		result = f.write((char*)&Preferences::wordChangeInterval, sizeof(Preferences::wordChangeInterval));
		result = f.write((char*)&Preferences::showTranslationInterval, sizeof(Preferences::showTranslationInterval));
		result = f.write((char*)&Preferences::showToolbar, sizeof(Preferences::showToolbar));
		
		// Main widget back color
		int_value = Preferences::mainWindowBackColor.red();
		result = f.write((char*)&int_value, sizeof(int_value));
		int_value = Preferences::mainWindowBackColor.green();
		result = f.write((char*)&int_value, sizeof(int_value));
		int_value = Preferences::mainWindowBackColor.blue();
		result = f.write((char*)&int_value, sizeof(int_value));
		
		// Main widget position
		int_value = Preferences::mainWindowPosition.x();
		result = f.write((char*)&int_value, sizeof(int_value));
		int_value = Preferences::mainWindowPosition.y();
		result = f.write((char*)&int_value, sizeof(int_value));
		
		// Main widget size
		int_value = Preferences::mainWindowSize.width();
		result = f.write((char*)&int_value, sizeof(int_value));
		int_value = Preferences::mainWindowSize.height();
		result = f.write((char*)&int_value, sizeof(int_value));
		
		// Word font
		int_value = Preferences::wordFont.pointSize();
		result = f.write((char*)&int_value, sizeof(int_value));
		bool_value = Preferences::wordFont.bold();
		result = f.write((char*)&bool_value, sizeof(bool_value));
		bool_value = Preferences::wordFont.italic();
		result = f.write((char*)&bool_value, sizeof(bool_value));
		bool_value = Preferences::wordFont.underline();
		result = f.write((char*)&bool_value, sizeof(bool_value));
		// transcriptionFont
		int_value = Preferences::transcriptionFont.pointSize();
		result = f.write((char*)&int_value, sizeof(int_value));
		bool_value = Preferences::transcriptionFont.bold();
		result = f.write((char*)&bool_value, sizeof(bool_value));
		bool_value = Preferences::transcriptionFont.italic();
		result = f.write((char*)&bool_value, sizeof(bool_value));
		bool_value = Preferences::transcriptionFont.underline();
		result = f.write((char*)&bool_value, sizeof(bool_value));
		// translationFont
		int_value = Preferences::translationFont.pointSize();
		result = f.write((char*)&int_value, sizeof(int_value));
		bool_value = Preferences::translationFont.bold();
		result = f.write((char*)&bool_value, sizeof(bool_value));
		bool_value = Preferences::translationFont.italic();
		result = f.write((char*)&bool_value, sizeof(bool_value));
		bool_value = Preferences::translationFont.underline();
		result = f.write((char*)&bool_value, sizeof(bool_value));
		
		// wordColor
		int_value = Preferences::wordColor.red();
		result = f.write((char*)&int_value, sizeof(int_value));
		int_value = Preferences::wordColor.green();
		result = f.write((char*)&int_value, sizeof(int_value));
		int_value = Preferences::wordColor.blue();
		result = f.write((char*)&int_value, sizeof(int_value));
		// transcriptionColor
		int_value = Preferences::transcriptionColor.red();
		result = f.write((char*)&int_value, sizeof(int_value));
		int_value = Preferences::transcriptionColor.green();
		result = f.write((char*)&int_value, sizeof(int_value));
		int_value = Preferences::transcriptionColor.blue();
		result = f.write((char*)&int_value, sizeof(int_value));
		// translationColor
		int_value = Preferences::translationColor.red();
		result = f.write((char*)&int_value, sizeof(int_value));
		int_value = Preferences::translationColor.green();
		result = f.write((char*)&int_value, sizeof(int_value));
		int_value = Preferences::translationColor.blue();
		result = f.write((char*)&int_value, sizeof(int_value));
		f.close();
	} else {
		qDebug() << "Unable to write settings file";
	}

	Preferences::forbiddenWords.Save("./Known words.txt");
	Preferences::favouriteWords.Save("./Favourite words.txt");
}

/* static */
void Preferences::Load() {
	QFile f("./settings.cfg");
	
	int int_temp;
	bool bool_temp;
	QString str_temp;
	qint64 result = 0;
	QString t;
	char text;
	
	
	if (f.open(QIODevice::ReadOnly)){
		result = f.read((char*)&bool_temp, sizeof(bool_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::firstTime = bool_temp;
		
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::wordShowDuration = int_temp;
	
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::wordChangeInterval = int_temp;
	
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::showTranslationInterval = int_temp;
	
		result = f.read((char*)&bool_temp, sizeof(bool_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::showToolbar = bool_temp;
	
	
		// Main widget back color
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::mainWindowBackColor.setRed(int_temp);
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::mainWindowBackColor.setGreen(int_temp);
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::mainWindowBackColor.setBlue(int_temp);
		
	
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::mainWindowPosition.setX(int_temp);
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::mainWindowPosition.setY(int_temp);
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::mainWindowSize.setWidth(int_temp);
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::mainWindowSize.setHeight(int_temp);
		
		
		// Word font
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::wordFont.setPointSize(int_temp);
		result = f.read((char*)&bool_temp, sizeof(bool_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::wordFont.setBold(bool_temp);
		result = f.read((char*)&bool_temp, sizeof(bool_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::wordFont.setItalic(bool_temp);
		result = f.read((char*)&bool_temp, sizeof(bool_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::wordFont.setUnderline(bool_temp);
		
	
	
		// transcriptionFont
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::transcriptionFont.setPointSize(int_temp);
		
		result = f.read((char*)&bool_temp, sizeof(bool_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::transcriptionFont.setBold(bool_temp);
		
		result = f.read((char*)&bool_temp, sizeof(bool_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::transcriptionFont.setItalic(bool_temp);
		
		result = f.read((char*)&bool_temp, sizeof(bool_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::transcriptionFont.setUnderline(bool_temp);
	
		// translationFont
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::translationFont.setPointSize(int_temp);
		
		result = f.read((char*)&bool_temp, sizeof(bool_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::translationFont.setBold(bool_temp);
		
		result = f.read((char*)&bool_temp, sizeof(bool_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::translationFont.setItalic(bool_temp);
		
		result = f.read((char*)&bool_temp, sizeof(bool_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::translationFont.setUnderline(bool_temp);
		
	
		// wordColor
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::wordColor.setRed(int_temp);
		
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::wordColor.setGreen(int_temp);
		
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::wordColor.setBlue(int_temp);
		
	
		// transcriptionColor
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::transcriptionColor.setRed(int_temp);
		
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::transcriptionColor.setGreen(int_temp);
		
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::transcriptionColor.setBlue(int_temp);
		
		
		// translationColor
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::translationColor.setRed(int_temp);
		
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::translationColor.setGreen(int_temp);
		
		result = f.read((char*)&int_temp, sizeof(int_temp));
		if (result == -1) {
			Preferences::Defaults();
			f.close();
			return;
		}
		Preferences::translationColor.setBlue(int_temp);
	
		f.close();

	} else {
		qDebug() << "Unable to read settings file";
		Preferences::Defaults();
	}
	
	Preferences::forbiddenWords.Load("./Known words.txt");
	Preferences::favouriteWords.Load("./Favourite words.txt");
	Preferences::favouriteWords.Substract(Preferences::forbiddenWords); // forbidden list has higher priority
}

/*static*/
void Preferences::Defaults() {
	qDebug() << "Loading default values";
	Preferences::firstTime = true;
	Preferences::mainWindowPosition = QPoint(0, 0);
	Preferences::mainWindowSize = QSize(200, 200);
	Preferences::showToolbar = true;
	Preferences::wordShowDuration = 15000;
	Preferences::wordChangeInterval = 30000;
	Preferences::showTranslationInterval = 5000;
	Preferences::mainWindowBackColor.setRgb(255, 255, 168);
}


